"""
Flask工程的主模块: 创建Flask应用程序对象
"""

from . import create_flask_app

class DefaultSettings(object):
    SECRET_KEY = "fewregqkbrjhlewbghjrgwkv"

class DefaultSettings2(object):
    SECRET_KEY = "fewregqkbrjhlewbghjrgwkvvferwger"
    REDIS_URL = "redis://127.0.0.1:6379/0"


# 1、创建flask的应用程序对象
app = create_flask_app(DefaultSettings)


# 2、定义视图
# GET /hello  --->   helloworld
@app.route('/hello', methods=['get', 'post'])
def helloworld():
    return "Hello World!"



# 写一个视图，打印出当前app，所有的路由规则

@app.route('/', methods=['get'])
def show_rules():
    # 如何通过app对象，来获取路由映射?!
    # 值：
    # [
    #   <Rule '/hello' (POST, OPTIONS, HEAD, GET) -> helloworld>,
    #   <Rule '/' (HEAD, OPTIONS, GET) -> show_rules>,
    #   <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>
    # ]
    # 类型：<class 'werkzeug.routing.Map'>
    # print(app.url_map, type(app.url_map))

    # map_iter = <list_iterator object at 0x10cb771d0>
    # 类型：<class 'list_iterator'>
    # map_iter = app.url_map.iter_rules()
    # print("map_iter: ", map_iter, type(map_iter))

    # return_list = []
    # for rule in app.url_map.iter_rules():
        # Rule对象: <Rule '/hello' (POST, OPTIONS, HEAD, GET) -> helloworld>
        # print("请求的路径：", rule.rule, "视图函数: ", rule.endpoint)
        # return_list.append({
            # "rule": rule.rule,
            # "endpoint": rule.endpoint
        # })

    # print(return_list)
    # import json
    # return json.dumps(return_list),{"Content-Type": "application/json"}

    # 获得路由规则映射的迭代器对象
    map_iter = app.url_map.iter_rules()

    # {
    #   '/hello': 'helloworld',
    #   '/': 'show_rules',
    #   '/static/<path:filename>': 'static'
    # }
    return_dict = {rule.rule:rule.endpoint for rule in map_iter}
    print(return_dict)

    import json
    return json.dumps(return_dict),{"Content-Type": "application/json"}



# 在FLask新版中，直接运行应用程序对象所在的主模块；已经废弃；
# if __name__ == "__main__":
#     # 3、运行测试服务器
#     app.run(host='127.0.0.1', port=5000, debug=True)